package com.watermeter.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.oa.android.R;
import com.watermeter.android.adapter.DcbAdapter;
import com.watermeter.android.entry.Rows;
import com.watermeter.android.utils.Util;
import com.watermeter.android.view.SelectDatePopupWindow;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.Calendar;
import java.util.List;


/**
 * 待超线路表
 *
 * @author -zouqh
 */
public class DcbActivity extends Activity implements OnClickListener {

    private ImageView close;
    private TextView title,rigth_text;
    private ImageView rigthIcon;
    private ListView listView;
    private DcbAdapter adapter;
    private List<Rows> rowsList;
    public DbManager.DaoConfig daoConfig;

    public int isFlash=0;
    public static DcbActivity dcbActivity;
    private Calendar calendar ;//日历
    private int yeas;//年
    private String mouth;//月
    private String time;
    private List<String> timeArr;
    private SelectDatePopupWindow datePop;
    private String strTime[]=new String[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dai_cb_activity);
        dcbActivity=this;
        close = (ImageView) findViewById(R.id.close);
        title = (TextView) findViewById(R.id.title_text);
        rigth_text = (TextView) findViewById(R.id.ritgh_text);
        rigthIcon =(ImageView)findViewById(R.id.ritgh_icon);
        rigthIcon.setVisibility(View.VISIBLE);
        rigthIcon.setImageResource(R.drawable.c_titlebar_zoomicn_normal);
        listView = (ListView) findViewById(R.id.listview);
        title.setText("待抄线路");
//       rigth_text.setVisibility(View.VISIBLE);
        close.setOnClickListener(this);
        rigthIcon.setOnClickListener(this);
//        daoConfig = new DbManager.DaoConfig();
        getRoute();
        if(rowsList!=null&&rowsList.size()>0){
            adapter=new DcbAdapter(this,rowsList);
            listView.setAdapter(adapter);
        }else{
            Toast.makeText(this,"你当前还没有抄表线路！",Toast.LENGTH_SHORT).show();
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(DcbActivity.this,RouteItemActivity.class);
                intent.putExtra("id",rowsList.get(position).getID());
                intent.putExtra("moth",rowsList.get(position).getMOTH());
                startActivity(intent);
            }
        });
        calendar = Calendar.getInstance();
        yeas=calendar.get(Calendar.YEAR);
        int m=calendar.get(Calendar.MONTH)+1;
        if(m<10){
            mouth="0"+m;
        }else{
            mouth=m+"";
        }
        time=yeas+mouth;
        rigth_text.setText(time);
        strTime[0]=time;
        strTime[1]=Util.getMosOne(yeas,m);
        strTime[2]=Util.getMosTwo(yeas,m);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isFlash!=0){
            isFlash=0;
            Util.printLog("DcbActivity---------onResume",isFlash+"");
            getRoute();
            adapter=new DcbAdapter(this,rowsList);
            listView.setAdapter(adapter);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent intent;
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;

            case R.id.ritgh_icon:
                startActivity(new Intent(this,SeachActivity.class));
                break;

            default:
                break;
        }
    }



    /**
     * 获取下载线路
     */
    private void getRoute() {
         DbManager manager = x.getDb(daoConfig);
        try {
            //创建对象，用来生成表
           rowsList= manager.selector(Rows.class).findAll();
//            Selector<Lis> limit = manager.selector(Lishi.class).o;

            if(null!=rowsList&&rowsList.size()>0){
                Util.printLog("DcbActivity---------getRoute",rowsList.size()+"");
//                lishis.addAll(lis);
                System.out.println(rowsList.get(0).getCBY());
//                adpter.notifyDataSetChanged();

            }

        } catch (DbException e) {
            e.printStackTrace();
        }
    }




}
